<template>
  <container-item-wrapper :widget="widget">
    <div :key="widget.id" class="table-container" v-show="!widget.options.hidden">
      <el-checkbox-group v-model="checkedList">
        <el-checkbox
          :label="widget.id"
          :value="widget.id"
          :disabled="shouldDisable(widget.id)"
          @click.stop="mRadioInput(widget.id)"
          v-if="parentWidget ? parentWidget.type != 'table-cell' : true"
          v-show="this.$route.query.btnType || sendIsIndicator"
        >
          <span style="color: black; font-weight: 550; font-size: 15px"
            >{{ widget.id }}（点击进行选择）</span
          >
        </el-checkbox>
      </el-checkbox-group>
      <table :ref="widget.id" class="table-layout" :class="[customClass]">
        <tbody>
          <tr v-for="(row, rowIdx) in widget.rows" :key="row.id">
            <template v-for="(colWidget, colIdx) in row.cols">
              <table-cell-item
                v-if="!colWidget.merged"
                :widget="colWidget"
                :key="colIdx"
                :parent-list="widget.cols"
                :row-index="rowIdx"
                :col-index="colIdx"
                :parent-widget="widget"
              >
                <!-- 递归传递插槽！！！ -->
                <template v-for="slot in Object.keys($slots)" v-slot:[slot]="scope">
                  <slot :name="slot" v-bind="scope" />
                </template>
              </table-cell-item>
            </template>
          </tr>
        </tbody>
      </table>
    </div>
  </container-item-wrapper>
</template>

<script>
import emitter from '@/utils/emitter';
import i18n from '../../../utils/i18n';
import refMixin from '../../../components/form-render/refMixin';
import ContainerItemWrapper from './container-item-wrapper';
import TableCellItem from './table-cell-item';
import containerItemMixin from './containerItemMixin';
import { mapState, mapMutations } from 'vuex';

export default {
  name: 'table-item',
  componentName: 'ContainerItem',
  mixins: [emitter, i18n, refMixin, containerItemMixin],
  components: {
    ContainerItemWrapper,
    TableCellItem,
  },
  computed: {
    ...mapState({
      checkedList: state => state.common.checkedList,
    }),
    shouldDisable() {
      return (val) => {
        if(this.$route.query.variableView){
          return true;
        }else if (this.$route.query.btnType === '2' && this.checkedList.length >= 1) {
          // 当路由参数为2 且 已有选中项时
          // 只禁用未选中的项
          return !this.checkedList.includes(val)
        }
        return false
      }
    },
  },
  props: {
    widget: Object,
  },
  inject: ['refList', 'sfRefList', 'globalModel'],
  data() {
    return {
      mRadio: false,
      mRadioList: [],
      sendIsIndicator:false,
    };
  },
  created() {
    this.initRefList();
  },
  mounted() {
    this.sendIsIndicator = localStorage.getItem('sendIsIndicator');
  },
  beforeUnmount() {
    this.unregisterFromRefList();
  },
  methods: {
    mRadioInput(val) {
      if(this.$route.query.variableView) return;
      // 新增：条件拦截
      if (this.shouldDisable(val) && !this.checkedList.includes(val)) return;
      localStorage.removeItem('sendCheckedTitle');
      this.checkedList = JSON.parse(localStorage.getItem('checkedList'));
      !this.checkedList ? (this.checkedList = []) : '';
    
     let index = this.checkedList.indexOf(val)
     index == -1 ? this.checkedList.push(val) : this.checkedList.splice(index, 1);
      
      this.checkedList = [...new Set(this.checkedList)];
      localStorage.setItem('checkedList', JSON.stringify(this.checkedList));
      localStorage.setItem('sendCheckedTitle', val);
    },
  },
};
</script>

<style lang="scss" scoped>
div.table-container {
  table.table-layout {
    width: 100%;
    table-layout: fixed;
    border-collapse: collapse;
  }
}
</style>
